package oss

import (
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"yunpan/cache/log"
	cfg "yunpan/config"
)


var ossCli *oss.Client

func Client() *oss.Client{
	if ossCli != nil {
		return ossCli
	}

	ossCli, err := oss.New(cfg.OSSEndpoint, cfg.OSSAccessKeyID, cfg.OSSAccessKeySecret)
	if err != nil {
		log.Ob().Warn("alioss connect err"+err.Error())
		return nil
	}
	return ossCli
}

func Bucket() *oss.Bucket {
	cli := Client()
	if cli != nil {
		bucket, err := cli.Bucket(cfg.OSSBucket)
		if err != nil {
			log.Ob().Warn(err.Error())
			return nil
		}
		return bucket
	}
	return nil
}

func DownloadURL(objKey string) string {
	signURL, err := Bucket().SignURL(objKey, oss.HTTPGet, 3600)
	if err != nil {
		log.Ob().Warn(err.Error())
		return ""
	}
	return signURL
}
